
--[[
    Aprs4G by BG2LBF - 混合定位管理
]]
local use_gps_time = 0
local use_lbs_time = 0
-- 防抖/节流参数
local GPS_NOFIX_TRY = 180      -- 首轮GPS尝试窗口（秒，原来是60）
local SWITCH_COOLDOWN = 90      -- 模式切换冷却时间（秒）
local last_switch_ts = 0

local is_first = true

local currentDecayValue = 0     -- 当前衰减值
local decayRate = 2             -- 衰减率为2
local maxDecayTimes = 32        -- 最大衰减倍数
local decayFunc

-- 间隔衰减计算，衰减率为2,最大衰减倍数32
local function calcDecay(initialValue)  
    currentDecayValue = initialValue  
    return function()  
        local oldValue = currentDecayValue  
        if currentDecayValue < initialValue * maxDecayTimes then
            currentDecayValue = currentDecayValue * decayRate              
        end
        return oldValue  
    end  
end 
-- 重置衰减值
function MIX_ResetDecay()
    currentDecayValue = 60
end

sys.taskInit(function()
    sys.waitUntil("LOC_GPS_LBS")
    sys.wait(1000)
    decayFunc = calcDecay(60)
    sys.publish("LOC_GPS")
    while true do
        if use_lbs_time == 0 and use_gps_time == 0 then
            -- 开启GPS搜星
            if is_first then
                is_first = false
            else
                sys.publish("POS_STATE_INITGPS")
            end
            use_gps_time = os.time()
            GPS_FIXED = false
        elseif use_lbs_time == 0 and use_gps_time ~= 0 then
            -- GPS中
            if (os.time() - use_gps_time > GPS_NOFIX_TRY) and (GPS_FIXED == false) and (os.time() - last_switch_ts > SWITCH_COOLDOWN) then
                -- 在切到 LBS 前尝试一次 AGNSS 注入，给 GPS 再 15s 机会
                if aprscfg and aprscfg.AGNSS_ENABLE == 1 then
                    sys.publish("POS_EXEC_AGNSS")
                    sys.wait(15000)
                end
                if GPS_FIXED then
                    -- 已经锁定，继续使用 GPS，并刷新计时
                    use_gps_time = os.time()
                else
                    -- 仍未锁定，切换至 LBS
                    log.info("MIX_LOC", "使用gps超过60s，未定位 ，则关闭GPS，切换到基站/wifi定位")
                    sys.publish("POS_STATE_CLOSEGPS")
                    sys.publish("LOC_LBS")
                    sys.publish("POS_STATE_INITLBS")
                    use_lbs_time = os.time()
                    use_gps_time = 0
                    last_switch_ts = os.time()
                end
            end
        elseif use_lbs_time ~= 0 and use_gps_time == 0 then
            -- LBS中
            if (os.time() - use_lbs_time > currentDecayValue) and (os.time() - last_switch_ts > SWITCH_COOLDOWN) then
                -- 使用基站超过120s，切换回GPS ，关闭基站/wifi定位
                log.info("MIX_LOC", "使用基站超过"..currentDecayValue.."s，切换回GPS ，关闭基站/wifi定位")
                decayFunc()
                sys.publish("POS_STATE_CLOSELBS")
                use_gps_time = 0
                use_lbs_time = 0
                last_switch_ts = os.time()
            end
        else
            -- 异常情况，重置数据
            log.error("MIX_LOC", "异常情况，重置数据")
            use_gps_time = 0
            use_lbs_time = 0
            GPS_FIXED = false
            MIX_ResetDecay()
            -- 不再直接重启，避免运行中抖动导致频繁重启
            sys.wait(500)
        end

        sys.wait(10000)
    end
end)

sys.subscribe("LOC_GPS_FIXED", function()
    MIX_ResetDecay()
end)

sys.subscribe("LOC_GPS_LOSE", function()
    -- 仅在GPS路径中更新use_gps_time，避免与LBS路径并存导致"异常情况"分支
    if use_lbs_time == 0 then
        use_gps_time = os.time()
    end
end)
